package com.example.canteen.controller;

import com.example.canteen.Response;
import com.example.canteen.dao.Dish;
import com.example.canteen.dto.DishDTO;
import com.example.canteen.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;

    @PostMapping("/all")
    public Response<Set<DishDTO>> getAllDishes() {
        return Response.newSuccess(dishService.getAllDishes());
    }

    @PostMapping("/add")
    public Response<Long> addDish(@RequestBody DishDTO dishDTO) {
        return Response.newSuccess(dishService.addDish(dishDTO, dishDTO.getCategoryName()));
    }

    @PostMapping("/delete")
    public void deleteDishByName(@RequestBody DishDTO dishDTO) {
        dishService.deleteDishByName(dishDTO.getName());
    }

    @PostMapping("/update")
    public Response<DishDTO> updateDish(@RequestBody DishDTO dishDTO) {
        return Response.newSuccess(dishService.updateDish(dishDTO.getId(), dishDTO.getName(), dishDTO.getCategoryName(), dishDTO.getComponents()));
    }
}
